from enum import auto
import dash
from dash import dcc, html, dash_table
import dash_bootstrap_components as dbc
# from .modals import  create_new_repair_modal ,create_repair_complete_modal
from modules.repair.modals import create_repair_respond_modal,create_upload_proof_modal,create_new_repair_modal ,create_repair_complete_modal
from models import Product,RepairRecord

# 更新维修状态选项
status_options = [
    {'label': '待响应', 'value': '待响应'},
    {'label': '维修中', 'value': '维修中'},
    {'label': '已修复', 'value': '已修复'},
    {'label': '已取消', 'value': '已取消'},
]




def create_repair_layout():
    # 应用 equipment-page 类，移除内联背景色和边框
    layout = html.Div([
      
        dcc.Store(id='repair-details-record-id-store'),
        dcc.Store(id='repair-refresh-flag-addnew'),
        dcc.Store(id='repair-refresh-flag-answer'),
        dcc.Store(id='repair-refresh-flag-complete'),
        dcc.Store(id='repair-refresh-flag-cancel'),

        # 主体内容区域，包含三列
        html.Div([
            
            # --- 左侧筛选条件区域 ---
            html.Div([ # Outer column
                # 移除 filter-container 的内联 style
                html.Div([ # Inner container
                    html.H5("筛选条件", className="filter-title mb-3"), # 添加标题

                    # 筛选控件容器
                    html.Div([
                        html.Label("所属机构", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-org-filter', options=[], placeholder='选择机构', className="mb-2 filter-dropdown"),

                        html.Label("所属科室", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-dept-filter', options=[], placeholder='选择科室', className="mb-2 filter-dropdown", disabled=True),

                        html.Label("设备ID", className="filter-label"),
                        dbc.Input(id='repair-id-filter', placeholder='输入设备ID', type='number', className="mb-3 form-control"),
                        
                        html.Label("维修状态", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-status-filter', options=status_options, placeholder='选择维修状态', className="mb-2 filter-dropdown"),

                        html.Label("报修日期范围", className="filter-label"), # 添加类名
                        dcc.DatePickerRange(
                            id='repair-date-filter',
                            display_format='YYYY-MM-DD',
                            start_date_placeholder_text='开始日期',
                            end_date_placeholder_text='结束日期',
                            className="mb-2 w-100 date-picker-range" # 添加类名
                        ),

                        html.Label("关键词搜索", className="filter-label"), # 添加类名
                        dbc.Input(id='repair-keyword-filter', placeholder='产品名/编号...', type='text', className="mb-3 form-control"),

                        # --- 修改筛选条件为支持手动输入的多选下拉框 ---
                        html.Label("维修人员", className="filter-label"),
                        dcc.Dropdown(
                            id='repair-person-filter',
                            options=[],  # 初始为空，后续通过回调填充
                            placeholder='选择或输入维修人员 (可多选)',
                            multi=True,
                            className="mb-3 filter-dropdown" # 应用统一样式
                        ),

                        html.Label("报修人", className="filter-label"),
                        dcc.Dropdown(
                            id='repair-caller-filter',
                            options=[],  # 初始为空，后续通过回调填充
                            placeholder='选择或输入报修人 (可多选)',
                            multi=True,
                            className="mb-3 filter-dropdown" # 应用统一样式
                        ),
                        # --- 修改结束 ---

                        # 筛选和重置按钮
                        html.Div([
                            dbc.Button("筛选", id="repair-filter-button", color="primary", className="me-1 btn-sm"),
                            dbc.Button("重置", id="repair-reset-button", color="secondary", className="btn-sm")
                        ], className="d-flex justify-content-start filter-buttons") # 添加类名

                    ], className="filter-controls-container")

                ], className="filter-container p-3") # 移除内联 style
            ], className="filter-column", style={ # 保留宽度和 padding
                'border': '2px dashed #1a4a7a',
                'borderRadius': '8px',
                'padding': '16px',
                'marginRight': '16px',
                "width": "20%",
                # "padding": "0 5px",
                "height": "calc(100vh - 120px)" # 调整高度以适应可能的导航栏
            }),

            # --- 中间维修记录列表区域 ---
            html.Div([
                # 移除 list-container 的内联 style
                html.Div([
                    html.H5("维修记录列表", className="list-title mb-3"), # 添加标题

                    dash_table.DataTable(
                        id='repair-records-table', # 使用新 ID
                        columns=[
                            {'name': '设备ID', 'id': 'product_id', 'type': 'numeric'},
                            {'name': '设备名称', 'id': 'product_name', 'type': 'text'},
                            # {'name': '产品编号', 'id': 'product_code', 'type': 'text'},
                            {'name': '所属机构', 'id': 'org_shortname', 'type': 'text'},
                            {'name': '所属科室', 'id': 'department_name', 'type': 'text'},
                            {'name': '故障描述', 'id': 'fault_description', 'type': 'text'},
                            {'name': '报修时间', 'id': 'report_date', 'type': 'datetime'},
                            {'name': '维修状态', 'id': 'repair_status', 'type': 'text'},
                            # {'name': '维修人员', 'id': 'repair_person', 'type': 'text'},
                            # {'name': '操作', 'id': 'actions', 'type': 'text', 'presentation': 'markdown'}
                        ],
                        data=[],
                        page_size=15,
                        page_current=0,
                        page_action='custom',
                        # filter_action='custom', # 移除 filter_action，因为筛选在外部完成
                        sort_action='custom',
                        sort_mode='multi',
                        row_selectable='single',
                        selected_rows=[],
                        # 调整 style_table 高度
                        style_table={'overflowX': 'auto', 'height': 'calc(100vh - 200px)', 'overflowY': 'auto'},
                        style_cell={'textAlign': 'left', 'padding': '8px', 'whiteSpace': 'normal', 'height': 'auto'}, # 增加 padding
                        
                        markdown_options={"html": True}
                    ),
                    

                    # 将 Store 和 Feedback 移到表格下方
                    dcc.Store(id='repair-selected-record-id'),
                    html.Div(id='repair-action-feedback'),
                    dcc.Upload(
                        id='upload-work-order',
                        children=html.Div(['拖拽或 ', html.A('选择电子工单文件')]),
                        style={'width': '100%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px 0', 'display': 'none'}, # 初始隐藏
                        multiple=False
                    ),
                    html.Div(id='upload-output')

                ], className="list-container p-3") # 移除内联 style

            ], className="list-column", style={ # 保留宽度和 padding
                "width": "50%",
                 'border': '2px dashed #1a4a7a',
                'borderRadius': '8px',
                'padding': '16px',
                'marginRight': '16px',
               
                # "padding": "0 5px",
                "height": "calc(100vh - 120px)" # 调整高度以适应可能的导航栏
            }),

            # --- 右侧维修详细信息区域 ---
            html.Div([
                # 移除 detail-container 的内联 style
                html.Div([
                    html.H5("维修详细信息", className="detail-title mb-3"), # 添加标题

                    # 详细信息展示区域
                    html.Div(id='repair-details', children=[
                        html.P("请在中间列表中选择一条维修记录查看详细信息。", className="text-muted mt-4")
                    ], className="detail-content p-3") # 添加内边距

                ], className="detail-container p-3") # 移除内联 style

            ], className="detail-column", style={ # 保留宽度和 padding
                "width": "30%",
                'border': '2px dashed #1a4a7a',
                'borderRadius': '8px',
                'padding': '16px',
                'marginRight': '16px',
                
                # "padding": "0 5px",
                "height": "calc(100vh - 120px)" # 调整高度以适应可能的导航栏
            }),

        ], className="row-container", style={"display": "flex", "flexWrap": "nowrap", "margin": "0 -5px"}),
        
        # 维修响应Modal组件
        create_repair_respond_modal(),

      

        #维修记录页面添加新维修单Modal窗口
        create_new_repair_modal(),

        create_upload_proof_modal(),

        create_repair_complete_modal(),

        #取消维修确认框
        dbc.Modal(
            [
                dbc.ModalHeader("用户确认操作"),
                dbc.ModalBody("确认取消维修订单。"),
                dbc.ModalFooter([
                   
                    dbc.Button("否", id="repair-cancel-no", color="secondary", className="me-2"),
                    dbc.Button("确定", id="repair-cancel-yes", color="danger"),
                ], style={"background": "red"}),
            ],
            id="repair-cancel-confirm-modal",
            is_open=False,
            centered=True,  
            style={
                
                
                "border": "2px solid #1a4a7a",
                "borderRadius": "8px",
                "padding": "0",
                "boxShadow": "0 4px 8px rgba(0, 0, 0, 0.1)",
                "zIndex": "1000",
                "background": "red"
                }
        
        ),

        # 左下角新增报修按钮
        html.Div(
            dbc.Button("新增报修", id="new-repair-btn", color="danger", className="mt-2", style={"width": "120px", "height": "40px"}),
            style={"position": "absolute", "left": "30px", "bottom": "30px", "zIndex": 999}
        ),
    # ], className="equipment-page repair-page", style={"height": "calc(100vh - 50px)", "overflow": "hidden", "position": "relative"}) # 原来的样式
    ], className="equipment-page repair-page", style={"height": "calc(100vh - 50px)", "position": "relative"}) # <--- 临时移除 overflow: hidden
    return layout

def create_details_layout():
    # 应用 equipment-page 类，移除内联背景色和边框
    layout = html.Div([
      

        # 主体内容区域，包含三列
        html.Div([
            
            # --- 左侧筛选条件区域 ---
            html.Div([ # Outer column
                # 移除 filter-container 的内联 style
                html.Div([ # Inner container
                    html.H5("筛选条件", className="filter-title mb-3"), # 添加标题

                    # 筛选控件容器
                    html.Div([
                        html.Label("所属机构", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-org-filter', options=[], placeholder='选择机构', className="mb-2 filter-dropdown"),

                        html.Label("所属科室", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-dept-filter', options=[], placeholder='选择科室', className="mb-2 filter-dropdown", disabled=True),

                        html.Label("维修状态", className="filter-label"), # 添加类名
                        # 添加 filter-dropdown 类名
                        dcc.Dropdown(id='repair-status-filter', options=status_options, placeholder='选择维修状态', className="mb-2 filter-dropdown"),

                        html.Label("报修日期范围", className="filter-label"), # 添加类名
                        dcc.DatePickerRange(
                            id='repair-date-filter',
                            display_format='YYYY-MM-DD',
                            start_date_placeholder_text='开始日期',
                            end_date_placeholder_text='结束日期',
                            className="mb-2 w-100 date-picker-range" # 添加类名
                        ),

                        html.Label("关键词搜索", className="filter-label"), # 添加类名
                        dbc.Input(id='repair-keyword-filter', placeholder='产品名/编号...', type='text', className="mb-3 form-control"),

                        # --- 修改筛选条件为支持手动输入的多选下拉框 ---
                        html.Label("维修人员", className="filter-label"),
                        dcc.Dropdown(
                            id='repair-person-filter',
                            options=[],  # 初始为空，后续通过回调填充
                            placeholder='选择或输入维修人员 (可多选)',
                            multi=True,
                            className="mb-3 filter-dropdown" # 应用统一样式
                        ),

                        html.Label("报修人", className="filter-label"),
                        dcc.Dropdown(
                            id='repair-caller-filter',
                            options=[],  # 初始为空，后续通过回调填充
                            placeholder='选择或输入报修人 (可多选)',
                            multi=True,
                            className="mb-3 filter-dropdown" # 应用统一样式
                        ),
                        # --- 修改结束 ---

                        # 筛选和重置按钮
                        html.Div([
                            dbc.Button("筛选", id="repair-filter-button", color="primary", className="me-1 btn-sm"),
                            dbc.Button("重置", id="repair-reset-button", color="secondary", className="btn-sm")
                        ], className="d-flex justify-content-start filter-buttons") # 添加类名

                    ], className="filter-controls-container")

                ], className="filter-container p-3") # 移除内联 style
            ], className="filter-column", style={ # 保留宽度和 padding
                "width": "20%",
                "padding": "0 5px",
                "height": "calc(100vh - 70px)" # 调整高度以适应可能的导航栏
            }),

            # --- 中间维修记录列表区域 ---
            html.Div([
                # 移除 list-container 的内联 style
                html.Div([
                    html.H5("维修记录列表", className="list-title mb-3"), # 添加标题

                    dash_table.DataTable(
                        id='repair-records-table', # 使用新 ID
                        columns=[
                            # {'name': 'ID', 'id': 'id', 'type': 'numeric'},
                            {'name': '产品名称', 'id': 'product_name', 'type': 'text'},
                            # {'name': '产品编号', 'id': 'product_code', 'type': 'text'},
                            {'name': '所属机构', 'id': 'org_name', 'type': 'text'},
                            {'name': '所属科室', 'id': 'department_name', 'type': 'text'},
                            {'name': '报修时间', 'id': 'report_date', 'type': 'datetime'},
                            {'name': '维修状态', 'id': 'repair_status', 'type': 'text'},
                            {'name': '维修人员', 'id': 'repair_person', 'type': 'text'},
                            # {'name': '操作', 'id': 'actions', 'type': 'text', 'presentation': 'markdown'}
                        ],
                        data=[],
                        page_size=15,
                        page_current=0,
                        page_action='custom',
                        # filter_action='custom', # 移除 filter_action，因为筛选在外部完成
                        sort_action='custom',
                        sort_mode='multi',
                        row_selectable='single',
                        selected_rows=[],
                        # 调整 style_table 高度
                        style_table={'overflowX': 'auto', 'height': 'calc(100vh - 200px)', 'overflowY': 'auto'},
                        style_cell={'textAlign': 'left', 'padding': '8px', 'whiteSpace': 'normal', 'height': 'auto'}, # 增加 padding
                        # 移除 style_header 和 style_data_conditional，让 CSS 文件控制
                        # style_header={...},
                        # style_data_conditional=[...],
                        markdown_options={"html": True}
                    ),
                    # 分页信息可以放在这里，如果需要的话
                    # html.Div(id='repair-pagination-info', className="pagination-info mt-2")

                    # 将 Store 和 Feedback 移到表格下方
                    dcc.Store(id='repair-selected-record-id'),
                    html.Div(id='repair-action-feedback'),
                    dcc.Upload(
                        id='upload-work-order',
                        children=html.Div(['拖拽或 ', html.A('选择电子工单文件')]),
                        style={'width': '100%', 'height': '60px', 'lineHeight': '60px', 'borderWidth': '1px', 'borderStyle': 'dashed', 'borderRadius': '5px', 'textAlign': 'center', 'margin': '10px 0', 'display': 'none'}, # 初始隐藏
                        multiple=False
                    ),
                    html.Div(id='upload-output')

                ], className="list-container p-3") # 移除内联 style

            ], className="list-column", style={ # 保留宽度和 padding
                "width": "50%",
                "padding": "0 5px",
                "height": "calc(100vh - 70px)" # 调整高度
            }),

            # --- 右侧维修详细信息区域 ---
            html.Div([
                # 移除 detail-container 的内联 style
                html.Div([
                    html.H5("维修详细信息", className="detail-title mb-3"), # 添加标题

                    # 详细信息展示区域
                    html.Div(id='repair-details', children=[
                        html.P("请在中间列表中选择一条维修记录查看详细信息。", className="text-muted mt-4")
                    ], className="detail-content p-3") # 添加内边距

                ], className="detail-container p-3") # 移除内联 style

            ], className="detail-column", style={ # 保留宽度和 padding
                "width": "30%",
                "padding": "0 5px",
                "height": "calc(100vh - 70px)" # 调整高度
            }),

        ], className="row-container", style={"display": "flex", "flexWrap": "nowrap", "margin": "0 -5px"}),
        
       
        
    ], className="equipment-page repair-page", style={"height": "calc(100vh - 50px)", "overflow": "hidden"}),

       

    return layout

from modules.repair.modals import create_new_repair_modal


#扫二维码新增维修单
def create_repair_form_page2(id):
    # print(f"尝试查找设备ID: {id}")
    device = Product.query.get(id)
    # print(f"查找结果: {device}")
    if not device:
        return html.Div("设备不存在", style={"textAlign": "center", "marginTop": "50px"})

    return html.Div([
        dcc.ConfirmDialog(
                    id='call-repaire-dialog',
                    message=''
                ),
        html.H3("新增报修", style={"margin": "30px 0 20px 0", "textAlign": "center"}),
        dbc.Form([
            dbc.Row([
                dbc.Col([
                    dbc.Label("所属机构"),
                    dbc.Input(type="text", value=device.org_short_name_c, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=6),
                dbc.Col([
                    dbc.Label("所属科室"),
                    dbc.Input(type="text", value=device.department_name, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=6),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("设备名称"),
                    dbc.Input(type="text", value=device.product_name, disabled=True, style={"backgroundColor": "#e9ecef"}),
                ], width=12),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("故障现象", html_for="new-repair-fault2"),
                    dbc.Textarea(
                        id="new-repair-fault2",
                        placeholder="请描述故障现象",
                        style={"height": "100px"},
                        className="mb-3"
                    ),
                ], width=12),
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Label("叫修人姓名", html_for="new-repair-caller2"),
                    dbc.Input(
                        type="text",
                        id="new-repair-caller2",
                        placeholder="请输入叫修人姓名",
                        className="mb-3"
                    ),
                ], width=6),
            ]),
            # dbc.Alert(id="new-repair-alert2", color="danger", is_open=False, duration=4000),
            html.Div(
                [
                    dbc.Button("提交", id="new-repair-confirm2", color="primary", className="me-2"),
                    dbc.Button("放弃", id="new-repair-cancel2", color="secondary"),
                ],
                style={"textAlign": "center", "marginTop": "20px"}
            )
        ], style={"maxWidth": "600px", "margin": "0 auto"})
    ])
    